package com.songshuai.webapi.auth.service;


import com.songshuai.webapi.auth.dao.YxUsersMapper;
import com.songshuai.webapi.auth.dto.User;
import com.songshuai.webapi.baseapi.utils.RegexUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by yubo on 2018/11/14.
 */
@Service
public class UserServiceDetail implements UserDetailsService {

    @Autowired
    private YxUsersMapper userRepository;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("执行到UserDetails");

        if(StringUtils.isNotBlank(username)){
            int integer = RegexUtil.validPhoneOrMail(username);
            if(1==integer){
                return  userRepository.SelectAuthByMobile(username);
            }else if(2==integer){
                return userRepository.SelectAuthByEmail(username);
            }else{
                List<User> users = userRepository.SelectAuthByUserName(username);
                if(users.size()>0){
                    return users.get(0);
                }else{
                    return new User();
                }

            }
        }else{
            return new User();
        }


    }
}
